var app = angular.module('conversionTool', [ 'ui.bootstrap' ]);

var dataTypeMessage = "Please Choose A Data Type";

app.controller('setupDataAdaptorCtrl', [ '$scope', '$http', function($scope, $http) {

	$scope.dataType = dataTypeMessage;

	$scope.dataTypeItems = [ 'CSV', 'EXCEL' ];

	$scope.status = {
		isopen : false
	};

	$scope.toggled = function(open) {
		$log.log('Dropdown is now: ', open);
	};

	$scope.toggleDropdown = function($event) {
		$event.preventDefault();
		$event.stopPropagation();
		$scope.status.isopen = !$scope.status.isopen;
	};

	$scope.selectDataType = function(dataType) {
		$scope.dataType = dataType;
		
		switch ($scope.dataType) {
		case $scope.dataTypeItems[0]:
			$scope.actionURL = "csvType";
			break;
		case $scope.dataTypeItems[1]:
			$scope.actionURL = "excelType";
			break;
		case $scope.dataTypeItems[2]:
			$scope.actionURL = "xmlType";
			break;
		case $scope.dataTypeItems[3]:
			$scope.actionURL = "dataBaseType";
			break;
		}
		
	}

	$scope.save = function() {

		if ($scope.adaptorName.length == 0) {
			$scope.msg = "Please enter the adaptor name!";
			return false;
		} else if ($scope.dataType == dataTypeMessage) {
			$scope.msg = "Please enter a data Type!";
			return false;
		}

		$http.post('/rest/dataAdaptor/save', {
			adaptorName : $scope.adaptorName,
			dataType : $scope.dataType,
			description : $scope.description

		}).success(function(data) {
			debugger

			if (data != ""&&data!=undefined) {
				$scope.dataAdaptorId=data.id;
				document.getElementById('dataAdaptorId').value=data.id;
				
				document.getElementById('dataAdaptorForm').submit();
			}else {
				$scope.msg = "Server side error, please refresh page.";
			}

		}).error(function(data) {
			$scope.msg = "Server side error, please refresh page.";
			console.log(data);
		});

	}
	

} ]);
